#中国大学排行爬虫： http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html
import requests
from bs4 import BeautifulSoup as b
import xlwt
def saveExcel(sheetname,title,data,filename):
    wbt = xlwt.Workbook()
    sheet = wbt.add_sheet(sheetname)
    for i in range(len(title)):
        sheet.write(0, i, title[i])
    for m in range(len(data)):
        for n in range(len(data[m])):
            sheet.write(m+1, n, data[m][n])
    wbt.save(filename)

head = {
    'User-Agent': 'Mozilla/5.0'
                  ' (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
r = requests.get('http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html', headers = head)
r.encoding = 'utf-8'
s = b(r.text, 'html.parser')
lis_3=[]
for item in s.find_all('thead'):
    for item in item.find_all('tr'):
        for i in item.find_all('th'):
            # print(i.name)
            if i.string == None:
                for j in i.find('select'):
                    if j.name =='option':
                        # print(j.string)
                        lis_3.append(j.string)
            else:
                # print(i.string)
                lis_3.append(i.string)
    print(lis_3)

lis_2=[]
for item in s.find_all('tr','alt'):
    lis_1 =[]
    for i in item.find_all('td'):
        # print(i.string)
        lis_1.append(i.string)
    # print(lis_1)
    lis_2.append(lis_1)
# print(lis_2)


if __name__ == '__main__':
    title1 = lis_3
    data1 = lis_2
    sheetname1 = '大学'
    filename1 = '好大学数据.xls'
    saveExcel(sheetname1, title1, data1, filename1)